package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.dto.AdvimgDto;
import com.itheima.ydd.dto.AdvposPage;
import com.itheima.ydd.entity.Advimg;
import com.itheima.ydd.entity.Advpos;
import com.itheima.ydd.mapper.AdvimgMapper;
import com.itheima.ydd.mapper.AdvposMapper;
import com.itheima.ydd.service.AdvimgService;
import com.itheima.ydd.service.AdvposService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author maorf
 * @version 1.0
 */
@Service
public class AdvimgServiceImpl extends ServiceImpl<AdvimgMapper, Advimg> implements AdvimgService {
    @Autowired
    AdvposService advposService;
    @Override
    public AdvposPage getPage(String advimgdesc, String advimgpos, Integer page, Integer limit) {
        LambdaQueryWrapper<Advimg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(advimgdesc),Advimg::getAdvimgdesc,advimgdesc).eq(!StringUtils.isEmpty(advimgpos),Advimg::getAdvimgpos,advimgpos);
        Page<Advimg> iPage = new Page<>(page,limit);
        page(iPage,queryWrapper);
        Page<AdvimgDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(iPage,dtoPage,"records");
        List<AdvimgDto> list = new ArrayList<>();
        for (Advimg record : iPage.getRecords()) {
            AdvimgDto advimgDto = new AdvimgDto();
            advimgDto.setId(record.getId());
            advimgDto.setAdvimgdesc(record.getAdvimgdesc());
            advimgDto.setAdvimglink(record.getAdvimglink());
            advimgDto.setAdvimgsrc(record.getAdvimgsrc());
            advimgDto.setDefault_data(record.getDefault_data());
            advimgDto.setAdvimgpos(record.getAdvimgpos());
            Advpos advpos = advposService.getById(record.getAdvimgpos());
            advimgDto.setAdvposname(advpos.getAdvposname());
            list.add(advimgDto);
        }
        dtoPage.setRecords(list);
        AdvposPage advposPage = new AdvposPage();
        advposPage.setCount((int)iPage.getTotal());
        advposPage.setCurrentPage((int) iPage.getCurrent());
        advposPage.setPageSize((int) iPage.getSize());
        advposPage.setTotalPages((int) iPage.getPages());
        advposPage.setData(dtoPage.getRecords());

        return advposPage;


    }
}
